import 'package:flutter/material.dart';

///TabBarWidget 支持顶部和底部设置
class TabBarWidget extends StatefulWidget {
  ///底部TAB
  static const int TAB_BOTTOM = 1;

  ///顶部TAB
  static const int TAB_TOP = 2;

  final type;

  final List<Widget> tabItems;
  final List<Widget> tabViews;

  final Color backgroundColor;
  final Color indicatorColor;

  final Widget title;
  final Widget drawer;

  final Widget floatActionButton;

  final TabWidgetControl tabWidgetControl;
  final PageController pageController;

  final ValueChanged<int> pageChanged;

  const TabBarWidget({
    Key key,
    this.type,
    this.tabItems,
    this.tabViews,
    this.backgroundColor,
    this.indicatorColor,
    this.title,
    this.drawer,
    this.floatActionButton,
    this.tabWidgetControl,
    this.pageController,
    this.pageChanged,
  }) : super(key: key);

  @override
  _TabBarWidgetState createState() => _TabBarWidgetState();
}

class _TabBarWidgetState extends State<TabBarWidget> with SingleTickerProviderStateMixin {
  _TabBarWidgetState();

  TabController _tabBarControl;

  @override
  void initState() {
    super.initState();

    ///初始化时创建控制器
    ///通过 with SingleTickerProviderStateMixin 实现动画效果。
    _tabBarControl = TabController(length: widget.tabItems.length, vsync: this);
  }

  @override
  void dispose() {
    _tabBarControl.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.type == TabBarWidget.TAB_BOTTOM) {
      //底部TabBar
      return Scaffold(
        ///设置侧边滑出 drawer，不需要可以不设置
        drawer: widget.drawer,

        ///设置悬浮按键，不需要可以不设置
        ///
        floatingActionButton: widget.floatActionButton,
        //标题栏
        appBar: AppBar(
          backgroundColor: Theme.of(context).primaryColor,
          title: widget.title,
        ),

        ///页面主体，PageView，用于承载Tab对应的页面
        body: new TabBarView(
            controller: _tabBarControl,
            children: widget.tabViews,
        ),

        ///底部导航栏，也就是tab栏
        bottomNavigationBar: new Material(
          color: Theme.of(context).primaryColor,

          ///tabBar控件
          child: new TabBar(
            ///必须有的控制器，与pageView的控制器同步
            controller: _tabBarControl,

            ///每一个tab item，是一个List<Widget>
            tabs: widget.tabItems,
            indicatorColor: widget.indicatorColor,
          ),
        ),
      );
    }

    //顶部TabBar
    return new Scaffold(
      floatingActionButton: widget.floatActionButton,
      persistentFooterButtons: widget.tabWidgetControl == null ? [] : widget.tabWidgetControl.footerButton,
      appBar: new AppBar(
        backgroundColor: Theme.of(context).primaryColor,
        title: widget.title,
        bottom: new TabBar(
            controller: _tabBarControl,
            tabs: widget.tabItems,
            indicatorColor: widget.indicatorColor,
          //可滑动模式
//          isScrollable: true,
        ),
      ),
      body: new PageView(
        controller: widget.pageController,
        children: widget.tabViews,
        onPageChanged: (index) {
          _tabBarControl.animateTo(index);
          widget.pageChanged?.call(index);
        },
      ),
    );
  }
}

class TabWidgetControl {
  List<Widget> footerButton = [];
}
